#leetcode题目13：罗马转整数
#难度：简单 
#时间复杂度：O(n)
#空间复杂度：O(1)
#方法：模拟
#目前暂时还没有吃透
class Solution:
    def romanToInt(self, s: str) -> int:
        numerals_map={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
        res=0
        for i in range(len(s)):
            if i>0 and numerals_map[s[i]]>numerals_map[s[i-1]]:
                res+=numerals_map[s[i]]-2*numerals_map[s[i-1]]
            else:
                res+=numerals_map[s[i]]
        return res

#测试数据
s="III"
#预期输出：3
solution=Solution()
print(solution.romanToInt(s))

s="IV"
#预期输出：4
solution=Solution()
print(solution.romanToInt(s))

s="IX"
#预期输出：9
solution=Solution()
print(solution.romanToInt(s))

s="LVIII"
#预期输出：58
solution=Solution()
print(solution.romanToInt(s))

s="MCMXCIV"
#预期输出：1994
solution=Solution()
print(solution.romanToInt(s))